import {
  Body,
  Controller,
  Delete,
  Get,
  HttpException,
  HttpStatus,
  Param,
  Patch,
  Post,
  Req,
  ServiceUnavailableException,
  Session,
  UseGuards,
} from '@nestjs/common';
import { AccountService } from '../service/account.service';
import { Public } from 'src/utils/skip.jwt';
import { CreateAccountDto } from '../dto/account/create-account.dto';
import { UpdateAccountDto } from '../dto/account/update-account.dto';
import { LoginAccountDto } from '../dto/account/login-account.dot';
import { AuthService } from 'src/auth/auth.service';
import { LocalAuthGuard } from 'src/auth/local-auth.guard';
import { CaptchaService } from '../captcha/captcha.service';

@Controller('grocery/account')
export class AccountController {
  constructor(
    private readonly accountService: AccountService,
    private readonly authService: AuthService,
    private readonly captchaService: CaptchaService
  ) {}

  @Public()
  @Post('registry')
  async create(@Body() createAccountDto: CreateAccountDto) {
    if (createAccountDto.password !== createAccountDto.pwdRepeat) {
      throw new HttpException(
        '两次密码不一致，请重新输入',
        HttpStatus.BAD_REQUEST,
      );
    }

    const isExitUser = await this.accountService.findUser(
      createAccountDto.username,
    );
    if (isExitUser) {
      throw new HttpException(
        '当前用户名已经存在，请重新输入',
        HttpStatus.BAD_REQUEST,
      );
    }
    return this.accountService.create(createAccountDto);
  }

  @Public()
  @UseGuards(LocalAuthGuard)
  @Post('login')
  async login(@Body() loginAccountDto: LoginAccountDto, @Session() session) {
    try {
      if (!loginAccountDto.username) {
        throw new HttpException('账户不能为空', HttpStatus.BAD_REQUEST);
      }

      if (!loginAccountDto.password) {
        throw new HttpException('密码不能为空', HttpStatus.BAD_REQUEST);
      }

      // 校验验证码
      if (!this.captchaService.verifyCaptcha(loginAccountDto.verifyCode, session)) {
        return
      }

      const userInfo = await this.accountService.findUser(loginAccountDto.username);

      if (!userInfo) {
        throw new HttpException(
          '当前用户名不存在，请重新输入',
          HttpStatus.BAD_REQUEST,
        );
      }
      return this.authService.login(userInfo);
    } catch (e) {
      throw new ServiceUnavailableException(e);
    }
  }

  @Patch()
  async update(@Req() req, @Body() updateAccountDto: UpdateAccountDto) {
    if (updateAccountDto.password !== updateAccountDto.pwdRepeat) {
      throw new HttpException(
        '两次密码不一致，请重新输入',
        HttpStatus.BAD_REQUEST,
      );
    }
    return this.accountService.update(+req.user.userId, updateAccountDto);
  }

  // 注销，后期使用token进行注销
  @Delete()
  async remove(id: number) {
    return this.accountService.remove(id);
  }

  // @Roles(Role_Grocery.Admin, Role_Grocery.User) // 设置角色权限
  @Get('userInfo')
  async getUserInfo(@Req() req) {
    try {
      return this.accountService.findOne(+req.user.userId);
    } catch (e) {
      throw new ServiceUnavailableException();
    }
  }

  // @Public()
  @Get('info')
  async getInfo(@Req() req) {
    return 'test'
  }
}
